package com.gy.assistant.ui.search.adapter.holder;

import android.app.Activity;
import android.os.Handler;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.gy.assistant.entity.SearchInfo;
import com.gy.assistant.ui.search.adapter.OnItemClickListener;
import java.util.ArrayList;
import java.util.List;

public class SearchContentAdapter extends RecyclerView.Adapter {
    private OnItemClickListener onItemClickListener;
    public Handler handler;
    private List<SearchInfo> messageInfoList = new ArrayList<>();
    private Activity activity;
    private boolean isGlobalSearch;

    public SearchContentAdapter(Activity activity, boolean isGlobalSearch) {
        this.activity = activity;
        this.isGlobalSearch = isGlobalSearch;
    }

    public void addItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (!isGlobalSearch) {

            viewHolder = new ChatViewHolder(parent, onItemClickListener, activity);

        } else {
            viewHolder = new ConversationViewHolder(parent, onItemClickListener, activity);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        holder.itemView.setTag(position);

        SearchInfo messageInfo = messageInfoList.get(position);
        if (holder instanceof ChatViewHolder) {
            ChatViewHolder chat = (ChatViewHolder)holder;
            chat.setMessageInfo(messageInfo);
        } else if (holder instanceof ConversationViewHolder) {
            ConversationViewHolder conversationViewHolder = (ConversationViewHolder)holder;
            conversationViewHolder.setConversationInfo(messageInfo.getConversation());
        }
    }

    @Override
    public int getItemCount() {
        return messageInfoList.size();
    }

    @Override
    public long getItemId(int position) {
        return messageInfoList.get(position).hashCode();
    }

    public void add(SearchInfo messageInfo) { messageInfoList.add(0, messageInfo); }

    public SearchInfo getSearchInfo(int position){
        return messageInfoList.get(position);
    }

    public void set(SearchInfo info) {
        int position = 0;
        for (int i = 0; i < messageInfoList.size(); i++) {
            if (messageInfoList.get(i).getZIMMessage().getLocalMessageID() ==
                info.getZIMMessage().getLocalMessageID()) {
                position = i;
                break;
            }
        }
        if (position != 0) {
            messageInfoList.set(position, info);
            notifyItemChanged(position);
        }
    }

    public void refresh(List<SearchInfo> infos) {
        messageInfoList.clear();
        messageInfoList.addAll(infos);
        notifyDataSetChanged();
    }

    public void addSearchInfo(List<SearchInfo> infos) {
        messageInfoList.addAll(infos);
        notifyDataSetChanged();
    }
}
